# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>

_realname=binutils
_mingw_suff=mingw-w64-cross
pkgname=("${_mingw_suff}-${_realname}")
pkgver=2.30
pkgrel=2
pkgdesc="A set of programs to assemble and manipulate binary and object files"
arch=('i686' 'x86_64')
url="https://www.gnu.org/software/binutils/"
license=('GPL')
groups=("${_mingw_suff}-toolchain" "${_mingw_suff}")
depends=("libiconv" "zlib")
checkdepends=('dejagnu' 'bc')
makedepends=("libiconv" "zlib")
options=('staticlibs' '!distcc' '!ccache' '!buildflags')
#install=binutils.install
source=(https://ftp.gnu.org/gnu/binutils/binutils-${pkgver}.tar.bz2{,.sig}
        0001-enable-gold-on.mingw32.patch
        0002-check-for-unusual-file-harder.patch
        0010-bfd-Increase-_bfd_coff_max_nscns-to-65279.patch
        0100-binutils-2.30-msys2.patch
        0110-binutils-mingw-gnu-print.patch
        0200-remove-provide-qualifiers.patch)
sha256sums=('efeade848067e9a03f1918b1da0d37aaffa0b0127a06b5e9236229851d9d0c09'
            'SKIP'
            '1455bb98abfab4b04cce78671da65046718029c21e952a2e86384735a7e61ac8'
            '0d94acaf3b53c910651e16eff2f23c0b63eb94b79beab8b137f774f4078fb680'
            '604e76e0f702ced493ee22aa3c1768b4776b2008a7d70ae0dd35fe5be3522141'
            '3ee46b3cab9db31d086ee422cef61d9a45d0c5a96c897093c680bdb18f70b3ba'
            '0f145655d4f2aae3383e0c0269d0c47b8a7176144bf0595425fc38b7ebf53153'
            '40f124febb3ee60239988127cf16d6a4d1bd41b156db8ec843a6871492fcca28')

_targets="i686-w64-mingw32 x86_64-w64-mingw32" # armv7-w64-mingw32

prepare() {
  cd ${srcdir}/binutils-${pkgver}

  patch -p1 -i "${srcdir}"/0001-enable-gold-on.mingw32.patch
  patch -p1 -i "${srcdir}"/0002-check-for-unusual-file-harder.patch
  patch -p1 -i "${srcdir}"/0010-bfd-Increase-_bfd_coff_max_nscns-to-65279.patch

  patch -p1 -i "${srcdir}"/0100-binutils-2.30-msys2.patch
  patch -p1 -i "${srcdir}"/0110-binutils-mingw-gnu-print.patch

  # Upstream patches
  # https://sourceware.org/bugzilla/show_bug.cgi?id=22762
  patch -p1 -i "${srcdir}"/0200-remove-provide-qualifiers.patch

  #do not install libiberty
  #sed -i 's/install_to_$(INSTALL_DEST) //' libiberty/Makefile.in
  # hack! - libiberty configure tests for header files using "$CPP $CPPFLAGS"
  sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" libiberty/configure

}

build() {
  for _target in ${_targets}; do
    mkdir -p ${srcdir}/binutils-build-${_target} && cd ${srcdir}/binutils-build-${_target}

    if [ "${_target}" != "armv7-w64-mingw32" ]; then
      local _conf='--enable-lto'
    else
      local _conf=''
    fi

    if [ "${_target}" = "x86_64-w64-mingw32" ]; then
     _conf+=' --enable-64-bit-bfd'
    fi

    ${srcdir}/binutils-${pkgver}/configure \
      --build=${CHOST} \
      --host=${CHOST} \
      --target=${_target} \
      --prefix=/opt \
      --disable-werror \
      --with-libiconv-prefix=/usr \
      $_conf

    make
  done
}

check() {
  for _target in ${_targets}; do
    cd ${srcdir}/binutils-build-${_target}

    # unset LDFLAGS as testsuite makes assumptions about which ones are active
    # do not abort on errors - manually check log files
    make LDFLAGS="" -k check || true
  done
}

package() {
  for _target in ${_targets}; do
    cd ${srcdir}/binutils-build-${_target}
    make DESTDIR=${pkgdir} install

    # Remove unwanted files
    #rm ${destdir}/usr/share/info/{configure,standards}.info

    # Add some useful headers
    #install -m644 ${srcdir}/binutils-${pkgver}/include/libiberty.h ${pkgdir}/opt/include
    #install -m644 ${srcdir}/binutils-${pkgver}/include/demangle.h ${pkgdir}/opt/include
  done
}
